package cn._51doit.flink.day03;

import org.apache.flink.api.common.functions.RichMapFunction;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.sink.RichSinkFunction;

public class ForwardDemo {

    public static void main(String[] args) throws Exception {

        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        //spark
        DataStreamSource<String> lines1 = env.socketTextStream("localhost", 8888);

        SingleOutputStreamOperator<String> upper1 = lines1.map(new RichMapFunction<String, String>() {
            @Override
            public String map(String s) throws Exception {
                int index = getRuntimeContext().getIndexOfThisSubtask();
                return s +  " -> map : " + index ;
            }
        });

        //forward即，直传，有算子链，在同一个subtask中传递，如果并行度一样，与默认效果一样
        DataStream<String> res = upper1.forward();

        res.addSink(new RichSinkFunction<String>() {
            @Override
            public void invoke(String value, Context context) throws Exception {
                int index = getRuntimeContext().getIndexOfThisSubtask();
                System.out.println(value + " -> sink : " + index);
            }
        });

        env.execute();

    }
}
